﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.RenderPipelineManager
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Collections.Generic;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Rendering
{
  /// <summary>
  ///   <para>Render Pipeline manager.</para>
  /// </summary>
  public static class RenderPipelineManager
  {
    internal static RenderPipelineAsset s_CurrentPipelineAsset;
    private static List<Camera> s_Cameras = new List<Camera>();
    private static string s_currentPipelineType = "Built-in Pipeline";
    private const string s_builtinPipelineName = "Built-in Pipeline";
    private static RenderPipeline s_currentPipeline = (RenderPipeline) null;

    /// <summary>
    ///   <para>Returns the active RenderPipeline.</para>
    /// </summary>
    public static RenderPipeline currentPipeline
    {
      get => RenderPipelineManager.s_currentPipeline;
      private set
      {
        RenderPipelineManager.s_currentPipelineType = value != null ? value.GetType().ToString() : "Built-in Pipeline";
        RenderPipelineManager.s_currentPipeline = value;
      }
    }

    public static event Action<ScriptableRenderContext, List<Camera>> beginContextRendering;

    public static event Action<ScriptableRenderContext, List<Camera>> endContextRendering;

    public static event Action<ScriptableRenderContext, Camera[]> beginFrameRendering;

    public static event Action<ScriptableRenderContext, Camera> beginCameraRendering;

    public static event Action<ScriptableRenderContext, Camera[]> endFrameRendering;

    public static event Action<ScriptableRenderContext, Camera> endCameraRendering;

    public static event Action activeRenderPipelineTypeChanged;

    /// <summary>
    ///   <para>Indicate when Render Pipeline switch is in progress.</para>
    /// </summary>
    public static bool pipelineSwitchCompleted
    {
      get
      {
        return RenderPipelineManager.s_CurrentPipelineAsset == GraphicsSettings.currentRenderPipeline && !RenderPipelineManager.IsPipelineRequireCreation();
      }
    }

    public static event Action activeRenderPipelineCreated;

    public static event Action activeRenderPipelineDisposed;

    internal static void BeginContextRendering(
      ScriptableRenderContext context,
      List<Camera> cameras)
    {
      Action<ScriptableRenderContext, Camera[]> beginFrameRendering = RenderPipelineManager.beginFrameRendering;
      if (beginFrameRendering != null)
        beginFrameRendering(context, cameras.ToArray());
      Action<ScriptableRenderContext, List<Camera>> contextRendering = RenderPipelineManager.beginContextRendering;
      if (contextRendering == null)
        return;
      contextRendering(context, cameras);
    }

    internal static void BeginCameraRendering(ScriptableRenderContext context, Camera camera)
    {
      Action<ScriptableRenderContext, Camera> beginCameraRendering = RenderPipelineManager.beginCameraRendering;
      if (beginCameraRendering == null)
        return;
      beginCameraRendering(context, camera);
    }

    internal static void EndContextRendering(ScriptableRenderContext context, List<Camera> cameras)
    {
      Action<ScriptableRenderContext, Camera[]> endFrameRendering = RenderPipelineManager.endFrameRendering;
      if (endFrameRendering != null)
        endFrameRendering(context, cameras.ToArray());
      Action<ScriptableRenderContext, List<Camera>> contextRendering = RenderPipelineManager.endContextRendering;
      if (contextRendering == null)
        return;
      contextRendering(context, cameras);
    }

    internal static void EndCameraRendering(ScriptableRenderContext context, Camera camera)
    {
      Action<ScriptableRenderContext, Camera> endCameraRendering = RenderPipelineManager.endCameraRendering;
      if (endCameraRendering == null)
        return;
      endCameraRendering(context, camera);
    }

    [RequiredByNativeCode]
    internal static void OnActiveRenderPipelineTypeChanged()
    {
      Action pipelineTypeChanged = RenderPipelineManager.activeRenderPipelineTypeChanged;
      if (pipelineTypeChanged == null)
        return;
      pipelineTypeChanged();
    }

    [RequiredByNativeCode]
    internal static void HandleRenderPipelineChange(RenderPipelineAsset pipelineAsset)
    {
      if (RenderPipelineManager.s_CurrentPipelineAsset == pipelineAsset)
        return;
      RenderPipelineManager.CleanupRenderPipeline();
      RenderPipelineManager.s_CurrentPipelineAsset = pipelineAsset;
    }

    [RequiredByNativeCode]
    internal static void CleanupRenderPipeline()
    {
      if (RenderPipelineManager.currentPipeline == null || RenderPipelineManager.currentPipeline.disposed)
        return;
      Action pipelineDisposed = RenderPipelineManager.activeRenderPipelineDisposed;
      if (pipelineDisposed != null)
        pipelineDisposed();
      RenderPipelineManager.currentPipeline.Dispose();
      RenderPipelineManager.s_CurrentPipelineAsset = (RenderPipelineAsset) null;
      RenderPipelineManager.currentPipeline = (RenderPipeline) null;
      SupportedRenderingFeatures.active = new SupportedRenderingFeatures();
    }

    [RequiredByNativeCode]
    private static string GetCurrentPipelineAssetType()
    {
      return RenderPipelineManager.s_currentPipelineType;
    }

    [RequiredByNativeCode]
    private static void DoRenderLoop_Internal(
      RenderPipelineAsset pipe,
      IntPtr loopPtr,
      List<Camera.RenderRequest> renderRequests,
      AtomicSafetyHandle safety)
    {
      RenderPipelineManager.PrepareRenderPipeline(pipe);
      if (RenderPipelineManager.currentPipeline == null)
        return;
      ScriptableRenderContext context = new ScriptableRenderContext(loopPtr, safety);
      RenderPipelineManager.s_Cameras.Clear();
      context.GetCameras(RenderPipelineManager.s_Cameras);
      if (renderRequests == null)
        RenderPipelineManager.currentPipeline.InternalRender(context, RenderPipelineManager.s_Cameras);
      else
        RenderPipelineManager.currentPipeline.InternalRenderWithRequests(context, RenderPipelineManager.s_Cameras, renderRequests);
      RenderPipelineManager.s_Cameras.Clear();
    }

    internal static void PrepareRenderPipeline(RenderPipelineAsset pipelineAsset)
    {
      RenderPipelineManager.HandleRenderPipelineChange(pipelineAsset);
      if (!RenderPipelineManager.IsPipelineRequireCreation())
        return;
      RenderPipelineManager.currentPipeline = RenderPipelineManager.s_CurrentPipelineAsset.InternalCreatePipeline();
      Action renderPipelineCreated = RenderPipelineManager.activeRenderPipelineCreated;
      if (renderPipelineCreated != null)
        renderPipelineCreated();
    }

    private static bool IsPipelineRequireCreation()
    {
      if (!((UnityEngine.Object) RenderPipelineManager.s_CurrentPipelineAsset != (UnityEngine.Object) null))
        return false;
      return RenderPipelineManager.currentPipeline == null || RenderPipelineManager.currentPipeline.disposed;
    }
  }
}
